<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类别" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键参数" prop="keyParameters">
        <el-input
          v-model="queryParams.keyParameters"
          placeholder="请输入关键参数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料级别" prop="itemLevel">
        <el-input
          v-model="queryParams.itemLevel"
          placeholder="请输入物料级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原厂型号" prop="originalModel">
        <el-input
          v-model="queryParams.originalModel"
          placeholder="请输入原厂型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注1" prop="remark1">
        <el-input
          v-model="queryParams.remark1"
          placeholder="请输入备注1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注2" prop="remark2">
        <el-input
          v-model="queryParams.remark2"
          placeholder="请输入备注2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简称" prop="shortName">
        <el-input
          v-model="queryParams.shortName"
          placeholder="请输入简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用量" prop="dosage">
        <el-input
          v-model="queryParams.dosage"
          placeholder="请输入用量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核" prop="shenhe">
        <el-input
          v-model="queryParams.shenhe"
          placeholder="请输入审核"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gyl:shenhe:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gyl:shenhe:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gyl:shenhe:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gyl:shenhe:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleButton(0)"
        >未处理</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleButton(1)"
        >通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleButton(2)"
        >未通过</el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <EasyTable ref="childRef" :tableData="shenheList" :columns="tableColumn"  @listen="handleSelectionChange" @changeList="rowsol">
      <vxe-column title="操作" width="250" show-overflow>
        <template #default="{ row }">
          <vxe-button type="text" icon="vxe-icon-edit" @click="handleUpdate(row)">编辑</vxe-button>
          <vxe-button type="text" icon="vxe-icon-delete" @click="handleDelete(row)">删除</vxe-button>
<!--          <vxe-button type="text" icon="vxe-icon&#45;&#45;info" @click="handleShenhe(row)">审核</vxe-button>-->
        </template>
      </vxe-column>
    </EasyTable>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料维护审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类别" prop="category">
          <el-input v-model="form.category" placeholder="请输入类别" />
        </el-form-item>
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="关键参数" prop="keyParameters">
          <el-input v-model="form.keyParameters" placeholder="请输入关键参数" />
        </el-form-item>
        <el-form-item label="物料级别" prop="itemLevel">
          <el-input v-model="form.itemLevel" placeholder="请输入物料级别" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="原厂型号" prop="originalModel">
          <el-input v-model="form.originalModel" placeholder="请输入原厂型号" />
        </el-form-item>
        <el-form-item label="备注1" prop="remark1">
          <el-input v-model="form.remark1" placeholder="请输入备注1" />
        </el-form-item>
        <el-form-item label="备注2" prop="remark2">
          <el-input v-model="form.remark2" placeholder="请输入备注2" />
        </el-form-item>
        <el-form-item label="简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入简称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用量" prop="dosage">
          <el-input v-model="form.dosage" placeholder="请输入用量" />
        </el-form-item>
        <el-form-item label="审核" prop="shenhe">
          <el-input v-model="form.shenhe" placeholder="请输入审核" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框   -->
<!--    <el-dialog title="审核" :visible.sync="shenhe" width="500px" append-to-body >-->
<!--      <el-radio-group v-model="radio" @change="changeHandler">-->
<!--        <el-radio :label="0">未处理</el-radio>-->
<!--        <el-radio :label="1">通过</el-radio>-->
<!--        <el-radio :label="2">不通过</el-radio>-->
<!--      </el-radio-group>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForShen">确 定</el-button>-->
<!--        <el-button @click="cancelForShen">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->

    <!--    修改某一列数据-->
    <el-dialog :visible.sync="shenhe" width="500px" append-to-body :title="title">
      <el-form label-width="80px">
        <el-form-item label="修改值" prop="category">
          <el-input v-model="list.afterName" placeholder="请输入修改值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitList">确 定</el-button>
        <el-button @click="cancelForShen">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShenhe, getShenhe, delShenhe, addShenhe, updateShenhe, PutShenhe } from '@/api/gyl/shenhe'
import EasyTable from '@/components/EasyTable/index.vue'
export default {
  name: "Shenhe",
  components: {
    EasyTable
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      radio:0,
      // 物料维护审核
      //表格数据
      shenheList: [],
      //修改列表参数
      list:{
        ids:[],
        //点击某一列的字段名称
        itemName:'',
        afterName:'',
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      shenhe:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        category: null,
        materialCode: null,
        materialName: null,
        keyParameters: null,
        itemLevel: null,
        brand: null,
        originalModel: null,
        remark1: null,
        remark2: null,
        shortName: null,
        dosage: null,
        shenhe: null
      },
      tableColumn: [
        //{ key: 0, type: 'seq', width: 60, fixed: null },
        { key: 1, type: 'checkbox', width: 50, fixed: null },
        { key: 2, field: 'id', title: '物料id', },
        { key: 3, field: 'category', title: '类别', },
        { key: 4, field: 'materialCode', title: '物料编码', },
        { key: 5, field: 'materialName', title: '物料名称', },
        { key: 6, field: 'keyParameters', title: '关键参数', },
        { key: 7, field: 'itemLevel', title: '物料级别', },
        { key: 8, field: 'brand', title: '品牌', },
        { key: 9, field: 'originalModel', title: '原厂型号', },
        { key: 10, field: 'remark1', title: '备注1', },
        { key: 11, field: 'remark2', title: '备注2', },
        { key: 12, field: 'shortName', title: '简称', },
        { key: 13, field: 'remark', title: '备注', },
        { key: 14, field: 'dosage', title: '用量', },
        { key: 15, field: 'shenhe', title: '审核', },
        { key: 16, field: 'describe', title: '描述', },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    getList() {
      this.loading = true;
      listShenhe(this.queryParams).then(response => {
        this.shenheList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        category: null,
        materialCode: null,
        materialName: null,
        keyParameters: null,
        itemLevel: null,
        brand: null,
        originalModel: null,
        remark1: null,
        remark2: null,
        shortName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        dosage: null,
        shenhe: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料维护审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShenhe(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料维护审核";
      });
    },
    handleShenhe(row){
      this.shenhe=true
    },
    changeHandler(value) {
      console.log('改变之后的值是:' + value)
    },
    submitForShen(){
    },
    cancelForShen(){
      this.shenhe=false
    },
    //获取表单数据
    rowsol(column){
      this.shenhe=true;
      this.list.afterName=''
      this.list.itemName = this.getKebabCase(column.column)
      this.list.ids= column.List
    },
    submitList(){
      PutShenhe(JSON.stringify(this.list)).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.opens = false;
        this.reset();
        this.getList();
      })
    },
    //将驼峰命名改成下划线
    getKebabCase(str) {
      let temp = str.replace(/[A-Z]/g, function(i) {
        return '_' + i.toLowerCase();
      })
      if (temp.slice(0,1) === '_') {
        temp = temp.slice(1);   //如果首字母是大写，执行replace时会多一个_，需要去掉
      }
      return temp;
    },
    handleButton(num){
      this.loading = true;
      listShenhe({shenhe:num}).then(response => {
        this.shenheList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShenhe(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShenhe(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物料维护审核编号为"' + ids + '"的数据项？').then(function() {
        return delShenhe(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gyl/shenhe/export', {
        ...this.queryParams
      }, `shenhe_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
